/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"k8stools/internal/memory"
	"k8stools/pkg/config"
)

// memoryCmd represents the memory command
var memoryCmd = &cobra.Command{
	Use:   "memory",
	Short: "获取k8s的内存使用情况",
	Long:  `获取 k8s 当前内存使用情况，获取的是瞬时值可以配合监控参考。`,
	Run: func(cmd *cobra.Command, args []string) {
		// 定义局部变量
		var memoryPath string
		var resourceType string
		
		// 获取标志值
		memoryPath, _ = cmd.Flags().GetString("file")
		resourceType, _ = cmd.Flags().GetString("type")
		
		c, err := config.ReadYaml(memoryPath)
		if err != nil {
			fmt.Println(err)
		}
		// 根据参数决定获取哪种资源类型的内存使用情况
		if resourceType == "deployment" || resourceType == "all" {
			memory.GetDeploymentMemory(c)
		}
		if resourceType == "daemonset" || resourceType == "all" {
			memory.GetDaemonSetMemory(c)
		}
		if resourceType == "statefulset" || resourceType == "all" {
			memory.GetStatefulSetMemory(c)
		}
		if resourceType == "job" || resourceType == "all" {
			memory.GetJobMemory(c)
		}
	},
}

func init() {
	rootCmd.AddCommand(memoryCmd)

	// Here you will define your flags and configuration settings.
	memoryCmd.Flags().StringP("file", "f", "config.yaml", "指定配置文件")
	memoryCmd.Flags().StringP("type", "t", "all", "指定资源类型 (deployment, daemonset, statefulset, job, all)")
}